Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.WFSCapabilities = function(config){
	
	this.urlvalue;
	
	this.geoMapPanel = new GBIF.GeoSpatial.MapComponent({
				height: 400
			,   width: 500
			,	region : 'center'
			,	border: false
			,	title : "Layer Preview"
			,	zoom:2
	});
	this.geoGridPanel=new GBIF.GeoSpatial.WFSCapabilitiesLayers ({	
			region : 'west'
		,   width: 400
		,	split: true
		, 	autoScroll: true 
		,	border: false
		
	});
	
	this.geoGridPanel.on('rowclick',this.loadLayer,this);
	
	/*
	this.wfs = new OpenLayers.Layer.Vector(
 				"WFS"
 			, 	{
 						strategies: [new OpenLayers.Strategy.BBOX()]
 					, 	protocol: new OpenLayers.Protocol.WFS({
 						//url:'http://downloads.wdpa.org/arcgis/services/EuroGEOSS/EuroGEOSS_WDPA/MapServer/WFSServer?',
 						url:url
 					, 	featureType: "states"
 					, 	featureNS: "http://www.openplans.org/topp"
				})
 		}); 
		*/

	Ext.apply(this,config,{
			layout: 'border'
		,	height: '550'
		,	border: true
		,	tbar: [{
						xtype:'label'
					,	style:"padding:0 5px"	
					,	text:'WFS Server URI: '	
					},{
						xtype: 'textfield'  
            		,	fieldLabel: 'WFS Server URI:'
					,	width:350
					,	ref: '../url'
		        	},{
            			text: 'Load'
					,	style:"padding:5px"
					,	handler: this.loadUrl
					, 	scope: this
        			}
					]
		,   proxyUrl: "../../resources/api/getcapabilities-proxy.php?url="
		,	items:[this.geoMapPanel,this.geoGridPanel]

	})
	GBIF.GeoSpatial.WFSCapabilities.superclass.constructor.call(this, config)
}
Ext.extend(GBIF.GeoSpatial.WFSCapabilities,Ext.Panel, {

		loadUrl: function(){
			this.urlvalue = this.url.getValue();
			this.clearLayer();
			if(this.urlvalue!=''){
					this.geoGridPanel.store.baseParams={
										url:this.urlvalue
										};
					this.geoGridPanel.store.load();
					this.url.setValue('');	
			}else{
				Ext.MessageBox.alert('Error','Please enter url');
			}
		}
		
		
	,	loadLayer:function(grid,index){
		
		var layer = grid.getStore().getAt(index).data.layer;
		/*
		 var wfsLayer = new OpenLayers.Layer.Vector(
        		layer.name
       	, 	{
             		strategies: [new OpenLayers.Strategy.Fixed()]
      			, 	protocol: new OpenLayers.Protocol.WFS({
          		//		url:"http://maps.opengeo.org/geowebcache/service/wfs?REQUEST=GetMap&VERSION=1.1.0&TYPENAME="+layer.protocol.featureType
						url:this.urlvalue
                  	,	'version': "1.0.0"
	            	,	'srsName': "EPSG:4326"
					, 	'featureType': layer.protocol.featureType
					,	'featureClass': OpenLayers.Feature.WFS
			//		,	featureNS: "http://www.openplans.org/topp"
			//		,	extractAtrributes:true 
			//		,	isBaseLayer:false
            	  })
				,	projection:  new OpenLayers.Projection("EPSG:4326")
		        ,	styleMap: new OpenLayers.StyleMap({
	        		strokeColor: "Green",
	        		strokeWidth: 40,
	        		strokeOpacity: 0.5,
	        		fillOpacity: 0.2
	    		}) 
			});*/
			OpenLayers.ProxyHost="/cgi-bin/proxy.cgi?url=";
			var wfs2 = new OpenLayers.Layer.Vector("WFS_",
                {strategies: [new OpenLayers.Strategy.Fixed()],
                projection: new OpenLayers.Projection("EPSG:4326"),
                protocol: new OpenLayers.Protocol.WFS({url: "../../resources/api/getcapabilities-proxy.php?url=http://myserver:81/geoserver/wfs",
                    featureNS: "http://geonetwork-opensource.org",
                    featureType: "mylayer",
                    geometryName: "SHAPE",
                    srsName: "EPSG:4326",
                    version: "1.0.0"})
                });
		//	this.geoMapPanel.map.removeLayer(this.geoMapPanel.map.layers[0]);
			this.geoMapPanel.map.addLayer(wfs2);
			
		}
		
	,	clearLayer:function(){
			Ext.each(this.geoMapPanel.map.layers,function(maplayer){
					if(maplayer.name != 'World Map' ){
						this.geoMapPanel.map.removeLayer(maplayer);
					}
			},this);
		}
		
})